package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ActivityPictureMapping;
import com.ruoyi.system.domain.DailyActivity;
import com.ruoyi.system.mapper.DailyActivityMapper;
import com.ruoyi.system.service.IDailyActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 创客动态Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-05
 */
@Service
public class DailyActivityServiceImpl implements IDailyActivityService
{
    @Autowired
    private DailyActivityMapper dailyActivityMapper;

    @Override
    public List<DailyActivity> selectHotActivities() {
        return dailyActivityMapper.selectHotActivities();
    }


    /**
     * 查询创客动态
     * 
     * @param activityId 创客动态主键
     * @return 创客动态
     */
    @Override
    public DailyActivity selectDailyActivityByActivityId(String activityId)
    {
        return dailyActivityMapper.selectDailyActivityByActivityId(activityId);
    }

    /**
     * 查询创客动态列表
     * 
     * @param dailyActivity 创客动态
     * @return 创客动态
     */
    @Override
    public List<DailyActivity> selectDailyActivityList(DailyActivity dailyActivity)
    {
        return dailyActivityMapper.selectDailyActivityList(dailyActivity);
    }

    /**
     * 新增创客动态
     *
     * @param dailyActivity 创客动态
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDailyActivity(DailyActivity dailyActivity)
    {
        // 生成 UUID 并保留其中的数字
        String uuid = UUID.randomUUID().toString().replaceAll("[^0-9]", ""); // 只保留数字部分
        String formattedUUID = "uuid-" + uuid.substring(0, 5); // 截取前5位数字

        // 设置活动 ID
        dailyActivity.setActivityId(formattedUUID);

        // 设置创建时间
        dailyActivity.setCreateTime(DateUtils.getNowDate());

        // 插入创客动态数据
        int rows = dailyActivityMapper.insertDailyActivity(dailyActivity);

        // 如果有活动图片映射操作，可以继续执行
        insertActivityPictureMapping(dailyActivity);

        return rows;
    }

    /**
     * 修改创客动态
     * 
     * @param dailyActivity 创客动态
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDailyActivity(DailyActivity dailyActivity)
    {
        dailyActivityMapper.deleteActivityPictureMappingByActivityId(dailyActivity.getActivityId());
        insertActivityPictureMapping(dailyActivity);
        return dailyActivityMapper.updateDailyActivity(dailyActivity);
    }

    /**
     * 批量删除创客动态
     * 
     * @param activityIds 需要删除的创客动态主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDailyActivityByActivityIds(String[] activityIds)
    {
        dailyActivityMapper.deleteActivityPictureMappingByActivityIds(activityIds);
        return dailyActivityMapper.deleteDailyActivityByActivityIds(activityIds);
    }

    /**
     * 删除创客动态信息
     * 
     * @param activityId 创客动态主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDailyActivityByActivityId(String activityId)
    {
        dailyActivityMapper.deleteActivityPictureMappingByActivityId(activityId);
        return dailyActivityMapper.deleteDailyActivityByActivityId(activityId);
    }

    /**
     * 新增配图管理信息
     * 
     * @param dailyActivity 创客动态对象
     */
    public void insertActivityPictureMapping(DailyActivity dailyActivity)
    {
        List<ActivityPictureMapping> activityPictureMappingList = dailyActivity.getActivityPictureMappingList();
        String activityId = dailyActivity.getActivityId();
        if (StringUtils.isNotNull(activityPictureMappingList))
        {
            List<ActivityPictureMapping> list = new ArrayList<ActivityPictureMapping>();
            for (ActivityPictureMapping activityPictureMapping : activityPictureMappingList)
            {
                activityPictureMapping.setActivityId(activityId);
                list.add(activityPictureMapping);
            }
            if (list.size() > 0)
            {
                dailyActivityMapper.batchActivityPictureMapping(list);
            }
        }
    }
}
